<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="产线/产线组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="line">
              <j-dict-select-tag type="list" v-model="model.line" dictCode="mes_line_cfg,LINE,LINE"
                                 placeholder="请选择产线或产线组" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="JDEID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jdeid">
              <a-input v-model="model.jdeid" placeholder="请输入JDEID"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="产品/产品组" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="partno">
              <j-popup
                v-model="model.partno"
                field="partno"
                org-fields="partno"
                dest-fields="partno"
                code="line_partno_pop"
                :multi="true"
                :param="model"
                @input="popupCallback"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="工单计划数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="qty">
              <a-input-number v-model="model.qty" placeholder="请输入工单计划数量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="计划起始时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planStart">
              <j-date placeholder="请选择工单计划起始时间" v-model="model.planStart" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="计划结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="planEnd">
              <j-date placeholder="请选择工单计划结束时间" v-model="model.planEnd" style="width: 100%" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'MesWoSrcForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      isEdit: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        line: [
          { required: true, message: '请输入产线或产线组!' }
        ],
        jdeid: [
          { required: true, message: '请输入JDEID!' }
        ],
        partno: [
          { required: true, message: '请输入产品或产品组!' }
        ],
        qty: [
          { required: true, message: '请输入工单计划数量!' }
        ]
      },
      url: {
        add: '/prod/mesWoSrc/add',
        edit: '/prod/mesWoSrc/edit',
        queryById: '/prod/mesWoSrc/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.isEdit = false
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      if (JSON.stringify(record) !== '{}') {
        this.isEdit = true
      }
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.isEdit) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }

      })
    },
    popupCallback(value, row) {
      this.model = Object.assign(this.model, row)
    }
  }
}
</script>